<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\AuthorTable;
use App\Admin\Repositories\WlGood;
use App\Models\Author;
use App\Models\GoodsCategory;
use App\Models\Location;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\RowActions\CopyAction;
use App\Models\WlGood AS WlGoodsModel;
use Illuminate\Support\Str;

class WlGoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WlGood(['goodsCategory', 'location','author']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('goods_name');
            $grid->column("goods_sn");
            $grid->column('location.name', "外联地");
            $grid->column('goodsCategory.name', "产品类别");
            $grid->column("author.name","作家");
            $grid->column("price","价格")->editable();
            $grid->column('barcode')->image(config("app.url")."/uploads", 100, 100);
            $grid->column('created_at');
            // 也可以这样
            $grid->disableBatchDelete();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('goods_name');
                $filter->equal('goods_category_id',"商品类别")
                    ->select(GoodsCategory::pluck("name","id")->toArray());
                $filter->equal("author_id","作家")
                    ->selectTable(AuthorTable::make())
                    ->title("作家")
                    ->model(Author::class,'id',"name");
                $filter->equal("location_id","外联地")
                    ->select(Location::pluck("name","id")->toArray());
                $filter->between('created_at', "添加日期")->datetime();
            });
            $grid->actions([new CopyAction(WlGoodsModel::class)]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WlGood(['goodsCategory', 'location', 'author']), function (Show $show) {
            $show->field('id');
            $show->field('goods_name');
            $show->field("goods_sn");
//            $show->field('goods_image')->image();
            $show->field('location.name', "地点");
            $show->field("spec", "规格");
//            $show->field("tags", "标签");
            $show->field("author.name","作家");
            $show->field('goods_category.name', "产品类别");
            $show->field('barcode')->image(config("app.url")."/uploads", 100, 100);
            $show->content()->unescape()->as(function ($content) {
                return $content;
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WlGood(), function (Form $form) {
            $form->display('id');
            $form->text('goods_name');
            $form->text("goods_sn");
//            $form->image('goods_image')->chunked()->uniqueName()->autoUpload();
            $form->select("location_id", "外联地")
                ->options(Location::orderBy("order","desc")->pluck("name", "id")->toArray())
                ->required();
            $form->select("goods_category_id", "商品类别")
                ->options(GoodsCategory::pluck("name", "id")->toArray())
                ->required();
            $form->select("author_id", "作家")
                ->options(Author::pluck("name", "id")->toArray());
//            $form->text("tags","标签")->help("多个用英文逗号分割,");
            $form->text("spec","规格");
            $form->text("price","价格");
            $form->editor("content", "描述")->rules("required", [
                "required" => "描述必须"
            ]);

            //商品编辑完毕，对商品的图册进行处理
            $form->saved(function (Form $form) {
                //秒杀处理，放入redis中
                $wl_goods = WlGoodsModel::find($form->getKey());
                //没有二维码，生成小程序二维码
                if (!$wl_goods->barcode) {
                    $miniProgram = \EasyWeChat::miniProgram(); // 小程序
                    $response = $miniProgram->app_code->getUnlimit($form->getKey(), [
                        "page" => "pages/goodslist/selfgoodsinfo",
                    ]);
                    // 或
                    if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                        $img_name = time() . Str::random(3) . ".jpg";
                        $filename = $response->saveAs(public_path() . '/uploads/', $img_name);
                        $wl_goods->barcode=$filename;
                        $wl_goods->save();
                    }
                }
//                return $form->location('/goods', '保存成功');
            });
        });
    }
}
